package cc.vv.btpartyjob.partymanager.activity

import android.os.Message
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.server.JniDataServer
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.AddProjectActivityDelegate
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.inter.CleanAddProjectItemClickInterface
import cc.vv.btpartyjob.partymanager.moudel.bean.AddProjectObj
import cc.vv.btpartyjob.partymanager.moudel.bean.SubmitProjectObj
import cc.vv.btpartyjob.partymanager.moudel.resp.SubmitUnitReportResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 添加项目
 * Created by Chen.qi on 2018/10/27.
 */
class AddProjectActivity : BaseActivityMVP<AddProjectActivityDelegate>(), CleanAddProjectItemClickInterface {

    /**
     * 项目类型
     */
    private var projectType: String? = null

    /**
     *申报时间
     */
    private var acceptTime: Long? = null
    /**
     *开始时间
     */
    private var startTime: Long = 0
    /**
     *结束时间
     */
    private var endTime: Long = 0


    /**
     * 整体项目预警时间
     */
    private var allWarningTime: Long? = null

    /**
     *整体项目完成时间
     */
    private var allCompleteTime: Long? = null

    /**
     *整体项目类型
     */
    private var allProjectState: Int? = null

    /**
     * 项目类型
     */
    private val listType = listOf("标准项目", "文本项目", "数值项目", "包装项目", "物料无关的项目")

    /**
     * 项目状态数据
     */
    private val listState = listOf("未开始", "进行中", "已完成")

    private var visibleRange: String? = null


    /**
     * 添加阶段数据集合
     */
    private var stageList: ArrayList<AddProjectObj>? = null

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("项目添加")
        stageList = ArrayList()
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //项目类型选择
            ResourceChooseServer.getWheelerWayDialog(this, "类型选择", handler, 5, listType)

        }, R.id.ll_addPr_projectType)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //申报时间
            ResourceChooseServer.chooseData(this, handler, 6, 3)

        }, R.id.ll_addPr_dataTime)
        viewDelegate?.setOnClickListener(View.OnClickListener {
            //项目开始时间
            ResourceChooseServer.chooseData(this, handler, 2, 3)

        }, R.id.ll_addPr_projectStart)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //项目结束时间
            ResourceChooseServer.chooseData(this, handler, 3, 3)

        }, R.id.ll_addPr_proEndTime)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预警时间
            ResourceChooseServer.chooseData(this, handler, 7, 3)

        }, R.id.ll_addPr_yjTime)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预计完成时间
            ResourceChooseServer.chooseData(this, handler, 8, 3)

        }, R.id.ll_addPr_endTime)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //项目状态
            ResourceChooseServer.getWheelerWayDialog(this, "状态选择", handler, 9, listState)

        }, R.id.ll_addPr_status)



        viewDelegate?.setOnClickListener(View.OnClickListener {
            //新增资源
            val obj = AddProjectObj()
            stageList!!.add(obj)
            viewDelegate?.setView(stageList!!, this)
        }, R.id.rl_addPr_addResource)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //可见范围

            ResourceChooseServer.getWheelerWayDialog(this@AddProjectActivity,
                    "可见范围", handler, 11, JniDataServer.getShowFanWeiData(), 4)

        }, R.id.ll_addPr_kejian)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            val projectName = viewDelegate?.getProjectName()
            val projectContent = viewDelegate?.getProjectContent()
            val projectPrincipalName = viewDelegate?.getProjectPrincipalName()
            val projectPrincipalPhone = viewDelegate?.getProjectPrincipalPhone()
            if (TextUtils.isEmpty(projectName) || TextUtils.isEmpty(projectContent)
                    || TextUtils.isEmpty(projectPrincipalName)
                    || TextUtils.isEmpty(projectPrincipalPhone)
                    || TextUtils.isEmpty(projectType)) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }
            if (acceptTime == null || allWarningTime == null || allCompleteTime == null || allProjectState == null) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            if (TextUtils.isEmpty(visibleRange)) {
                LKToastUtil.showToastShort("请选择可见范围")
                return@OnClickListener
            }

            if (startTime < 1 || endTime < 1) {
                LKToastUtil.showToastShort("请设置项目开始结束时间")
                return@OnClickListener
            }

            if (stageList!!.size > 0) {
                val isAll = (0 until stageList!!.size)
                        .map { stageList!![it] }
                        .none {
                            TextUtils.isEmpty(it.content) || it.warningTime == null
                                    || it.completeTime == null || it.state == null
                        }

                if (!isAll) {
                    LKToastUtil.showToastShort("项目阶段数据不完整，请补充完整")
                    return@OnClickListener
                }
            }

            val sub = SubmitProjectObj()
            sub.startTime = startTime
            sub.endTime = endTime
            sub.projectName = projectName
            sub.projectIntroduction = projectContent
            sub.projectType = projectType
            sub.principal = projectPrincipalName
            sub.mobile = projectPrincipalPhone
            sub.declarationTime = acceptTime
            if (stageList!!.size > 0) {
                sub.segmentationList = stageList
            }
            sub.warningTime = allWarningTime
            sub.completeTime = allCompleteTime
            sub.state = allProjectState
            sub.visibleRange = visibleRange
            PartyManagerServer.projectAdd(sub, true)
        }, R.id.rl_addPr_submit)
    }

    override fun cleanItem(pos: Int) {
        stageList!!.removeAt(pos)
        viewDelegate?.setView(stageList!!, this)
    }

    override fun changeContent(pos: Int, content: String?) {
        stageList!![pos].content = content
        stageList!![pos].stage = "${pos + 1}"
    }

    override fun changeYJtime(pos: Int, yJTime: String?) {
        val yJ = LKTimeUtil.getInstance().getLongTime(yJTime, "yyyy-MM-dd")
        stageList!![pos].warningTime = yJ
        stageList!![pos].stage = "${pos + 1}"
    }

    override fun changeCompleteTime(pos: Int, completeTime: String?) {
        val complete = LKTimeUtil.getInstance().getLongTime(completeTime, "yyyy-MM-dd")
        stageList!![pos].completeTime = complete
        stageList!![pos].stage = "${pos + 1}"
    }

    override fun changeProjectStatus(pos: Int, status: String?) {
        when (status!!) {
            "未开始" -> {
                stageList!![pos].state = 0
            }
            "进行中" -> {
                stageList!![pos].state = 1
            }
            "已完成" -> {
                stageList!![pos].state = 2
            }
        }
        stageList!![pos].stage = "${pos + 1}"
    }

    override fun getDelegateClass(): Class<AddProjectActivityDelegate> {
        return AddProjectActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {

            2 -> {
                //项目开始时间
                val dateString: String = msg.obj as String
                startTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setStartTime(dateString)
            }

            3 -> {
                //项目结束时间
                val dateString: String = msg.obj as String
                endTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setEndTime(dateString)
            }

            5 -> {
                //项目类型
                val wayString: String = msg.obj as String
                projectType = wayString
                viewDelegate?.setProjectType(wayString)
            }

            6 -> {
                //申报时间
                val dateString: String = msg.obj as String
                acceptTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setShenBaoTime(dateString)
            }
            7 -> {
                //预警时间
                val dateString: String = msg.obj as String
                allWarningTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setOverAllYJTime(dateString)

            }

            8 -> {
                //预计完成时间
                val dateString: String = msg.obj as String
                allCompleteTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setOverAllCompleteTime(dateString)
            }

            9 -> {
                //项目状态
                val wayString: String = msg.obj as String
                when (wayString!!) {
                    "未开始" -> {
                        allProjectState = 0
                    }
                    "进行中" -> {
                        allProjectState = 1
                    }
                    "已完成" -> {
                        allProjectState = 2
                    }
                }
                viewDelegate?.setOverAllStateSelect(wayString)
            }

            11 -> {
                val wayString: String = msg.obj as String
                viewDelegate?.setKeJian(wayString)
                when {
                    TextUtils.equals("本网格可见", wayString) -> visibleRange = "0"
                    TextUtils.equals("本单位可见", wayString) -> visibleRange = "1"
                    TextUtils.equals("本社区可见", wayString) -> visibleRange = "2"
                    TextUtils.equals("本街道可见", wayString) -> visibleRange = "3"
                    TextUtils.equals("宝塔区可见", wayString) -> visibleRange = "4"
                }

            }
        }
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SubmitUnitReportResponseObj -> {
                LKToastUtil.showToastShort("添加项目成功，请等待管理员审核")
                finish()
            }
        }
    }


}